package main

import "fmt"

// Definition for singly-linked list.
type ListNode struct {
	Val  int
	Next *ListNode
}

func middleNode(head *ListNode) *ListNode {
	//1.定义快慢指针
	fast := head //快指针
	slow := head //慢指针

	//2.移动快慢指针
	for fast != nil && fast.Next != nil {
		fast = fast.Next.Next //每次移动 2 个节点
		slow = slow.Next      //每次移动 1 个节点
	}
	return slow
}
func main() {
	head := &ListNode{Val: 1}
	head.Next = &ListNode{Val: 2}
	head.Next.Next = &ListNode{Val: 3}
	head.Next.Next.Next = &ListNode{Val: 4}
	head.Next.Next.Next.Next = &ListNode{Val: 5}
	head.Next.Next.Next.Next.Next = &ListNode{Val: 6}

	node := middleNode(head)
	fmt.Println(node.Val)
}
